class Manage::MessagesController < ApplicationController
  include Security::Session::Access
  include Utils::Url
  include UserData

  uses_tiny_mce(:options => {:theme => 'advanced',
                :browsers => %w{msie gecko opera},
                :theme_advanced_toolbar_location => "top",
                :theme_advanced_toolbar_align => "left",
                :theme_advanced_resizing => true,
                :theme_advanced_resize_horizontal => false,
                :paste_auto_cleanup_on_paste => true,
                :theme_advanced_buttons1 => %w{bold italic underline link unlink spellchecker},
                :theme_advanced_buttons2 => [],
                :theme_advanced_buttons3 => [],
                :plugins => %w{spellchecker  paste } })

  def list
    @message_pages, @messages = paginate(:message, :per_page => 10, :order => "expires_on desc")
  end

  def create
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => "list")
        return
      end
      params[:message].delete(:id)
      create_message_from_params
      @message.updated_by = current_updater_id
      if @message.save
        flash[:notice] = "Message successfully created"
        redirect_to(:action => "show", :id => @message.id)
      end
    else
      @message = Message.new
      @message.expires_on = Time.now + 2.weeks
    end
  end

  def update
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => "list")
        return
      end
      @message = Message.find(params[:message][:id])
      params[:message].delete(:id)
      update_message_from_params
      @message.updated_by = current_updater_id
      if @message.save
        flash[:notice] = "Message successfully updated"
        redirect_to(:action => "show", :id => @message.id)
      end
    else
      @message = Message.find(id_param)
    end
  end

  def show
    @message = Message.find(id_param)
  end

  def delete
    Message.find(id_param).destroy
    redirect_to(:action => "list")
  end

  private
    def create_message_from_params
      @message = Message.new(params[:message])
    rescue ActiveRecord::MultiparameterAssignmentErrors => e
      e.errors.each do |err|
        params[:message].each do |key,value|
          if key =~ /~#{err.attribute}/
            @message.errors.add(key,"invalid date entered") 
            params[:messages].delete(key)
          end
        end
      end
      @message = Message.new(params[:message])
    ensure
      gather_user_group_info(@message)
    end

    def update_message_from_params
      @message.attributes = params[:message]
    rescue ActiveRecord::MultiparameterAssignmentErrors => e
      e.errors.each do |err|
        params[:message].each do |key,value|
          if key =~ /~#{err.attribute}/
            @message.errors.add(key,"invalid date entered") 
            params[:messages].delete(key)
          end
        end
      end
      @message.attributes = params[:message]
    ensure
      gather_user_group_info(@message)
    end
end
